package com.cn.xiaonuo.tcrfinance.sysmgmt.controller;

import com.cn.xiaonuo.common.constant.SystemConstant;
import com.cn.xiaonuo.common.entity.SysLog;
import com.cn.xiaonuo.common.utils.StringUtils;
import com.cn.xiaonuo.common.validated.util.ValidatorUtils;
import com.cn.xiaonuo.core.annotion.BusinessLog;
import com.cn.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.core.pojo.login.SysLoginUser;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.pojo.response.SuccessResponseData;
import com.cn.xiaonuo.tcrfinance.sysmgmt.entity.SysRoleHpcEntity;
import com.cn.xiaonuo.tcrfinance.sysmgmt.enums.SystemEnum;
import com.cn.xiaonuo.tcrfinance.sysmgmt.param.SysRoleHpcParam;
import com.cn.xiaonuo.tcrfinance.sysmgmt.param.SysRoleReParam;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysRoleHpcService;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysRoleMenuHpcService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
public class SysRoleHpcController   {
	@Autowired
	private SysRoleHpcService sysRoleService;
	@Autowired
	private SysRoleMenuHpcService sysRoleMenuService;

	/**
	 * 角色列表
	 */
	@PostMapping("/api/sys/role/list")
	@ApiOperation(value = "角色列表", notes = "角色列表")
	public ResponseData list(SysRoleReParam sysRoleParam){
		SysLoginUser loginUser=(SysLoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//如果不是超级管理员，则只查询自己创建的角色列表
		if(loginUser.getId() != SystemConstant.SUPER_ADMIN){
			sysRoleParam.setCreateUserId(loginUser.getId());
		}
		return new SuccessResponseData(sysRoleService.queryPage(sysRoleParam));
	}

	/**
	 * 角色选择
	 */
	@PostMapping("/api/sys/role/select")
	@ApiOperation(value = "角色选择", notes = "角色选择")
	public ResponseData select(){
		Map<String, Object> map = new HashMap<>();

		//如果不是超级管理员，则只查询自己所拥有的角色列表
		SysLoginUser loginUser=(SysLoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(loginUser.getId() != SystemConstant.SUPER_ADMIN){
			map.put("create_user_id", loginUser.getId());
		}
		List<SysRoleHpcEntity> list = (List<SysRoleHpcEntity>) sysRoleService.listByMap(map);
		return new SuccessResponseData(list);
	}

	/**
	 * 角色信息
	 */
	@PostMapping("/api/sys/role/info")
	@ApiOperation(value = "角色信息", notes = "角色信息")
	public ResponseData info(@RequestBody SysRoleHpcParam sysRoleParam){
		long roleId=sysRoleParam.getRoleId();
		SysRoleHpcEntity role = sysRoleService.getById(roleId);

		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);
		return new SuccessResponseData(role);
	}

	/**
	 * 保存角色
	 */
	@SysLog("新增角色")
	@PostMapping("/api/sys/role/save")
	@ApiOperation(value = "新增角色", notes = "新增角色")
	public ResponseData save(@RequestBody SysRoleHpcParam role){
		if(StringUtils.isEmpty(role.getRoleName())){
			throw new ServiceException(SystemEnum.VALIDATE_CODE_EMPTY);
		}
		ValidatorUtils.validateEntity(role);
		SysLoginUser loginUser=(SysLoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		role.setCreateUserId(loginUser.getId());
		sysRoleService.saveRole(role);

		return new SuccessResponseData();
	}

	/**
	 * 修改角色
	 */
	@SysLog("修改角色")
	@BusinessLog(title = "修改角色", opType = LogAnnotionOpTypeEnum.UPDATE)
	@PostMapping("/api/sys/role/update")
	@ApiOperation(value = "修改角色", notes = "修改角色")
	public ResponseData update(@RequestBody SysRoleHpcParam role){
		ValidatorUtils.validateEntity(role);
		SysLoginUser loginUser=(SysLoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		role.setCreateUserId(loginUser.getId());
		sysRoleService.update(role);

		return new SuccessResponseData();
	}

	/**
	 * 删除角色
	 */
	@SysLog("删除角色")
	@BusinessLog(title = "删除角色", opType = LogAnnotionOpTypeEnum.UPDATE)
	@PostMapping("/api/sys/role/delete")
	@ApiOperation(value = "删除角色", notes = "删除角色")
	public ResponseData delete(@RequestBody SysRoleHpcParam sysRoleHpcParam){
		sysRoleService.deleteBatch(sysRoleHpcParam.getRoleIds());
		return new SuccessResponseData();
	}
}
